//
//  BJVideoViewController.m
//  BowJoy
//
//  Created by  on 2024/5/13.
//

#import "BJVideoViewController.h"
#import "BJVideoListTableViewCell.h"
#import "BJPlayVideoViewController.h"

@interface BJVideoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) NSMutableArray<BJAnchorWallModelData *> *records;
@property (weak, nonatomic) IBOutlet UILabel *titleL;

@end

@implementation BJVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleL.text = @"Video";
    self.titleL.font = [UIFont fontWithName:@"Silkscreen"size:20];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"BJVideoListTableViewCell" bundle:nil] forCellReuseIdentifier:@"BJVideoListTableViewCell"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //埋点
    NSMutableArray *dataList = [NSMutableArray array];
    NSDictionary *dict = @{
        @"page":@"video",
        @"duration":@(0),
        @"event":@"pv",
        @"tm":@([[BJNetManager shareNetManager] gs_getCurrentTimeToMilliSecond]),
    };
    [dataList addObject:dict];
    [[BJNetManager shareNetManager]postLogWithParam:dataList];
        
    NSArray *dataQueryList = [[BJFMDBVoiceManager sharedManager] queryAllData];
    if (dataQueryList.count>0) {
        self.records = [[NSMutableArray alloc]initWithArray:dataQueryList];
        [self.tableView reloadData];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.records.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BJVideoListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BJVideoListTableViewCell"];
    if (cell == nil){
        cell = [[BJVideoListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BJVideoListTableViewCell"];
    }
    
    BJAnchorWallModelData *model = self.records[indexPath.row];
    cell.model = model;
    kWeakSelf(weakSelf)

    cell.moreBtn.onClick(^{
        ReportBlock *reportBlock =[[ReportBlock alloc]initWithIsBlock:model.isFriend isFriend:model.isFriend userId:[model.userId longLongValue]];
        [reportBlock setFollowClickBlock:^(BOOL follow) {
            model.isFriend = follow;
            [[BJFMDBVoiceManager sharedManager] uptadeDataWithFrend:follow andUserId:model.userId];

            [weakSelf.tableView reloadData];
            if (follow) {
                [BJUserManager sharedManager].followingNum ++;
            }else{
                [BJUserManager sharedManager].followingNum --;
            }

        }];//关注
        [reportBlock setBlockClickBlock:^(BOOL block) {
            [weakSelf.records removeObject:model];
            [weakSelf.tableView reloadData];
            [[BJFMDBVoiceManager sharedManager] deleteDataWithUserId:model.userId];
            [[BJFMDBBlockManager sharedManager] insertWitModel:model];
            [BJUserManager sharedManager].blockNum ++;

        }];//拉黑
        
        [reportBlock show];

    });
    cell.followBtn.onClick(^{
        
        if (!model.isLike) {
            model.isLike = YES;
            model.followNum ++;
            [weakSelf.tableView reloadData];
        }else{
            model.isLike = NO;
            model.followNum --;
            [weakSelf.tableView reloadData];
        }
        [[BJFMDBVoiceManager sharedManager] uptadeDataWithLike:model.isLike andUserId:model.userId];
        [[BJFMDBVoiceManager sharedManager] uptadeDataWithFollowNum:model.followNum andUserId:model.userId];
        
    });
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BJPlayVideoViewController *vc = [[BJPlayVideoViewController alloc] init];
    vc.model = self.records[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
